<%--

    Document   : viewReport
    Created on : 02 3, 12, 4:57:29 PM
    Author     : pauld
--%>

<%@page import="Controller.FinanceManager"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Controller.AccountManager"%>
<%@page import="Controller.AccountManager"%>
<%@page import="Controller.UserManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String user = (String)session.getAttribute("user");
        String name = (String)session.getAttribute("name");
        
        //if user is is not logged in or is not an admin
        if(user == null || UserManager.isFinanceManager(user) != 1) {
            response.sendRedirect("../index.jsp");
        }
        //if the user is logged and is an admin in check for session integrity
        else if( (String)session.getAttribute("user") != null ){
        
            //check session integrity by comparing the hash of the session token
            String currSession = request.getRemoteAddr().concat( session.getId() );
            try{
                if( !UserManager.checkSessionIntegrity(currSession, session) ){
                    UserManager.logout(request);
                    response.sendRedirect("../login.jsp");
                }
            }catch(NoSuchAlgorithmException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(UnsupportedEncodingException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(Exception e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
        }
        out.println("hello world");
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="../css/960_24_col.css" />
        <link rel="stylesheet" href="../css/reset.css" />
        <link rel="stylesheet" href="../css/text.css" />
        <link rel="stylesheet" href="../css/style.css" />
        
        <title>FooBar Online Bookstore | Financial Report</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="index.jsp"><img src="../images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="../index.jsp">Home</a></li>
                            <li><a href="../main.jsp">Products</a>
                                <ul style="display: none;">
                                    <li><a href="../main.jsp?show=book">Books</a></li>
                                    <li><a href="../main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="../main.jsp?show=cd">CD</a></li>
                                    <li><a href="../main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="../main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <li><a href='admin/financePanel.jsp'>Sales Panel</a></li>
                            <li><a href="../about.jsp">About</a></li>
                            <li><a href="../contact.jsp">Contact Us</a></li>
                            <%
                            if(user == null){
                                out.println("<li><a href='login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='../logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <h1>Finance Panel</h1>
                <%
                ResultSet totalSales = FinanceManager.getTotalSales();

                if ( totalSales.next() ) { 
                    out.println("<span id='total-sales'><b>Total Sales: </b>" + totalSales.getString(1) + "</span>");
                }
                else{
                    out.println("<span id='total-sales'><b>Total Sales: </b>0</span>");
                }
                %>
                    
                <h3>Sales Per Product</h3>
                <table class="finance-table">
                    <th>Product</th>
                    <th>Total Sales</th>
                    <%
                    ResultSet salesPerProduct = FinanceManager.getSalesPerProduct();

                    while ( salesPerProduct.next() ) {
                        out.println("<tr>"
                        + "<td>" + salesPerProduct.getString(1) + "</td>"
                        + "<td>" + salesPerProduct.getString(2) + "</td>"
                        + "</tr>");
                    }
                    %>
                </table>
                    
                <h3>Sales Per Product Type</h3>
                <table class="finance-table">
                    <th>Product Type</th>
                    <th>Total Sales</th>
                    <%
                    ResultSet salesPerProductType = FinanceManager.getSalesPerProductType();

                    while (salesPerProductType.next()) { 
                        out.println("<tr>"
                        + "<td>" + salesPerProductType.getString(1) + "</td>"
                        + "<td>" + salesPerProductType.getString(2) + "</td>"
                        + "</tr>");
                    }
                    %>
                </table>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="../index.jsp">home</a></li>
                        <li><a href="../contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
